package id.co.rho.scoring.cid.client.mvc.compare;

import java.util.List;

import id.co.rho.scoring.cid.client.command.CommandDispatcherAsync;
import id.co.rho.scoring.cid.client.command.impl.ModelSearchCommandAction;
import id.co.rho.scoring.cid.client.command.impl.ModelSearchCommandResult;
import id.co.rho.scoring.cid.client.command.impl.ScoringModelCommand;
import id.co.rho.scoring.cid.client.command.impl.ScoringModelResult;
import id.co.rho.scoring.cid.client.command.impl.ModelSearchCommandAction.SearchType;
import id.co.rho.scoring.cid.client.mvc.AppEvents;
import id.co.rho.scoring.cid.client.mvc.model.ModelSearchView;
import id.co.rho.scoring.cid.client.security.Credential;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.store.ListStore;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;

public class CompareModelView extends View {

	private CommandDispatcherAsync dispatcher;
	private Credential credential;
	private CompareModelUI compareModelUI;

	@Inject
	public CompareModelView(CompareModelUI ui, Credential credential,
			CommandDispatcherAsync dispatcher) {
		super(null);
		this.compareModelUI = ui;
		controller = new ViewController();
		this.dispatcher = dispatcher;
		this.credential = credential;
	}

	@Override
	protected void handleEvent(AppEvent event) {
		if (event.getType().equals(AppEvents.CompareModelEvent)) {
			compareModelUI.show();
			compareModelUI.clearData();
			searchModel();
		}

	}

	@Override
	public void initialize() {
		super.initialize();
		compareModelUI.initialize();
		compareModelUI.getCompareButton().addSelectionListener(
				new SelectionListener<ButtonEvent>() {
					@Override
					public void componentSelected(ButtonEvent ce) {
						doCompareModel();
					}
				});
	}

	public void doCompareModel() {
		String combo1Val = null;
		String combo2Val = null;
		if (compareModelUI.getCombo1().getValue() != null) {
			combo1Val = compareModelUI.getCombo1().getValue().get("id")
					.toString();
		}

		if (compareModelUI.getCombo2().getValue() != null) {
			combo2Val = compareModelUI.getCombo2().getValue().get("id")
					.toString();
		}

		ScoringModelCommand cmd = new ScoringModelCommand();
		cmd.setModelId(compareModelUI.getModelId1());
		cmd.setModelId2(compareModelUI.getModelId2());
//		cmd.setModelId(Long.parseLong(combo1Val));
//		cmd.setModelId2(Long.parseLong(combo2Val));
		cmd.setCommandType(cmd.getCommandType().COMPARE);
		dispatcher.execute(cmd, new AsyncCallback<ScoringModelResult>() {

			@Override
			public void onFailure(Throwable arg0) {

			}

			@Override
			public void onSuccess(ScoringModelResult arg0) {
				compareModelUI.setModel(arg0.folder,arg0.folder2);
			}
		});
		

		
	}

	public void searchModel() {
		ModelSearchCommandAction action = new ModelSearchCommandAction();
		action.setSearchType(SearchType.VERSIONING);
		action.setName(null);
		dispatcher.execute(action,
				new AsyncCallback<ModelSearchCommandResult>() {
					@Override
					public void onFailure(Throwable caught) {
					}

					@Override
					public void onSuccess(ModelSearchCommandResult result) {
						List<ModelData> models = (List<ModelData>) result
								.getModels();
						ListStore<ModelData> modelStore = new ListStore<ModelData>();
						for (ModelData model : models) {
							modelStore.add(model);
						}
						compareModelUI.setModels(modelStore);
					}

				});

	}

	private class ViewController extends Controller {
		private ViewController() {
			registerEventTypes(AppEvents.CompareModelEvent);
		}

		@Override
		public void handleEvent(AppEvent event) {
			forwardToView(CompareModelView.this, event);
		}

	}

}
